package com.example.todo_app;

import java.util.ArrayList;
import java.util.List;

import com.example.todo_app.model.ListElement;

import de.htwds.emrolab.mada.activities.MadaArrayAdapter;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class SettingsActivity extends Activity {

	private CharSequence name;
	private CharSequence description;
	private CharSequence mock;
	private boolean prio;
	private MadaArrayAdapter adapter;
	private Intent intent;
	private ListElement listelement;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);

		final TextView textName = (TextView) findViewById(R.id.txt_name);
		final TextView textDescrip = (TextView) findViewById(R.id.txt_descrip);
		final Spinner spinnerPrio = (Spinner) findViewById(R.id.spinner_prio);
		int listIndex = getIntent().getIntExtra("ListIndex", -1);

		if (listIndex > -1) {
			adapter = MadaArrayAdapter.getInstance();
			listelement = (ListElement) adapter.getItem(listIndex);
			name = listelement.getName();
			description = listelement.getDescription();
//			mock = (listelement.isPrio()) ? getString(R.string.prio_high)
//					: getString(R.string.prio_low);
			TextView tv1 = (TextView) findViewById(R.id.txt_name);
			TextView tv2 = (TextView) findViewById(R.id.txt_descrip);
			Spinner spinner = (Spinner) findViewById(R.id.spinner_prio);
			tv1.setText(name);
			tv2.setText(description);
//			spinner.setSelection((listelement.isPrio() ? 1 : 0));
		} else {
			name = textName.getText();
			description = textDescrip.getText();
			mock = (CharSequence) spinnerPrio.getSelectedItem();
			listelement = null;
		}
		Button btnCreate = (Button) findViewById(R.id.btn_create);
		btnCreate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// name = (CharSequence) textName.getText();
				// description = (CharSequence) textDescrip.getText();
				// CharSequence mock = (CharSequence)
				// spinnerPrio.getSelectedItem();

				// listElement = new ListElement(name, description, prio);
				if (listelement != null) {
					//neue werte einlesen
					name = textName.getText();
					description = textDescrip.getText();
//					mock = (CharSequence) spinnerPrio.getSelectedItem();
					//speichern ins listenelement
					listelement.setName(name.toString());
					listelement.setDescription(description.toString());
					
//					listelement.setPrio(mock
//							.equals(getString(R.string.prio_high)) ? true
//							: mock.equals(getString(R.string.prio_low)) ? false
//									: null);
					
				} else {
					List stringData = new ArrayList<String>();
					stringData.add(name);
					stringData.add(description);
					stringData.add(mock);
					Intent intent = new Intent();
					intent.putStringArrayListExtra("ListObject",
							(ArrayList<String>) stringData);
					setResult(RESULT_OK, intent);
				}
				finish();
			}
		});
	}

	@Override
	public void onStop() {
		super.onPause();
		setResult(RESULT_CANCELED);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_settings, menu);
		return true;
	}
}
